<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\BaseController;
use App\Http\Requests\Admin\GoodsRequest;
use App\Models\Category;
use App\Models\Good;
use App\Transformers\GoodTransformer;
use Illuminate\Http\Request;

class GoodsController extends BaseController
{
    /**
     * 商品列表
     */
    public function index(Request $request)
    {
        //获取搜索条件
        $title = $request->input('title');
        //两种不同方式获取
        $category_id = $request->query('category_id');

        $is_on = $request->input('is_on',false);

        $is_recommend = $request->query('is_recommend',false);

        $goods = Good::when($title,function ($query) use ($title){
            $query->where('title','like',"%$title%");
        })
        ->when($category_id,function ($query) use ($category_id){
            $query->where('category_id',$category_id);
        })
        ->when($is_on !== false,function ($query) use ($is_on){
            $query->where('is_on',$is_on);
        })
        ->when($is_recommend !== false,function ($query) use ($is_recommend){
            $query->where('is_recommend',$is_recommend);
        })
        ->paginate(2);

        return $this->response->paginator($goods,new GoodTransformer());
    }

    /**
     * 添加商品
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(GoodsRequest $request)
    {
        // 对分类进行一下检查, 只能使用3级分类, 并且分类不能被禁用
        $category = Category::find($request->category_id);

        if(!$category) return $this->response->errorBadRequest('请选择分类');

        if($category->level != 3) return $this->response->errorBadRequest('只能使用三级分类');

        if($category->status !=1 ) return $this->response->errorBadRequest('当前分类已被禁用');


        $userid = auth('api')->id();
        // $insertData = $request->all();
        // $insertData['user_id'] = $userid;
        //追加user_id
        $request->offsetSet('user_id',$userid);

        Good::create($request->all());

        return $this->response->created();
    }

    /**
     * 商品详情
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show(Good $good)
    {
        return $this->response->item($good,new GoodTransformer());
    }

    /**
     * 编辑商品
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(GoodsRequest $request, Good $good)
    {
        // 对分类进行一下检查, 只能使用3级分类, 并且分类不能被禁用
        $category = Category::find($request->category_id);

        if(!$category) return $this->response->errorBadRequest('请选择分类');

        if($category->level != 3) return $this->response->errorBadRequest('只能使用三级分类');

        if($category->status !=1 ) return $this->response->errorBadRequest('当前分类已被禁用');

        $good->update($request->all());

        return $this->response->noContent();
    }

    /**
     * 上架商品
     */
    public function isOn(Good $good)
    {
        $good->is_on =  $good->is_on == 1 ? 0 : 1;
        $good->save();
        return $this->response->noContent();
    }

    /**
     * 推荐商品
     */
    public function isRecommend(Good $good)
    {
        $good->is_recommend =  $good->is_recommend == 1 ? 0 : 1;
        $good->save();
        return $this->response->noContent();
    }
}
